package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.UserDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CheckItemServiceImpl
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/8/20 16:00
 * @Version V1.0
 */
// dubbo（整合声明式事务处理1：配置applicationContext-tx.xml对类代理，2：@Service(interfaceClass = CheckItemService.class)）
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Override
    public User findUserByUsername(String username) {
        return userDao.findUserByUsername(username);
    }

    // 菜单2
    @Override
    public User findByMenuUserUsername(String username) {
        return userDao.findByMenuUserUsername(username);
    }

    @Override
    public void add(User user, Integer[] roleIds) {

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(user.getPassword());
        user.setPassword(encode);


        userDao.add(user);
        setCheckGroupCheckItem(user.getId(), roleIds);
    }

    @Override
    public void deleteById(Integer id) {
        List<Integer> list = userDao.finduserandroleByuserId(id);
        if (list.size()>0) {
            throw new RuntimeException("当前已被用户角色表关联，不能进行删除");
        }
        userDao.deleteById(id);
    }

    @Override
    public void edit(User user, Integer[] roleIds) {
        userDao.deleteRoleByUserId(user.getId());
        userDao.edit(user);
        setCheckGroupCheckItem(user.getId(), roleIds);
    }

   /* @Override
    public Map<String, Object> findById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        User user = userDao.findById(id);
        map.put("username", user.getUsername());
        map.put("telephone", user.getTelephone());
        map.put("gender", user.getGender());
        map.put("birthday", new SimpleDateFormat("yyyy-MM-dd").format(user.getBirthday()));
        map.put("remark", user.getRemark());
        map.put("id",user.getId());
        return map;
    }*/

    @Override
    public List<Integer> findRolesListByUserId(Integer id) {
        return userDao.findRolesListByUserId(id);
    }
   @Override
   public User findById(Integer id) {
       return userDao.findById(id);
   }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<User> page = userDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    private void setCheckGroupCheckItem(Integer id, Integer[] roleIds) {
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                Map<String, Object> map = new HashMap<>();
                map.put("user_id", id);
                map.put("role_id", roleId);
                userDao.setUserIdAndRoleIdMap(map);
            }
        }
    }
}
